
package com.jht.bletool2kotlin.activity.homepage.fragment.device.adapter

import android.content.Context
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.LinearLayout
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.jht.bletool2kotlin.R
import com.jht.bletool2kotlin.activity.homepage.fragment.device.DeviceFragment
import com.jht.bletool2kotlin.activity.homepage.fragment.device.bean.BLEDevice
import com.jht.bletool2kotlin.widget.ChangeColorTextView
import org.greenrobot.eventbus.EventBus
import java.util.*
import kotlin.collections.ArrayList

private const val TAG = "BleDeviceAdapter"

class BleDeviceAdapter(private val ctx: Context) :
    RecyclerView.Adapter<BleDeviceAdapter.BleDeviceViewHolder>() {

    private val bleDeviceList: MutableList<BLEDevice> = ArrayList()

    @Suppress("MemberVisibilityCanBePrivate")
    class BleDeviceViewHolder(itemView: View, viewType: Int) : RecyclerView.ViewHolder(itemView) {
        var mBtnDeviceName: TextView? = null
        var mBtnDeviceMac: TextView? = null
        var mTvBleDbm: ChangeColorTextView? = null
        var mBtnConnectBle: Button? = null
        var mLlBleLayoutParent: LinearLayout? = null

        init {
            if (viewType == R.layout.item_ble_device) {
                mBtnDeviceName = itemView.findViewById(R.id.btn_device_name)
                mBtnDeviceMac = itemView.findViewById(R.id.btn_device_mac)
                mTvBleDbm = itemView.findViewById(R.id.tv_ble_dbm)
                mBtnConnectBle = itemView.findViewById(R.id.btn_connect_ble)
                mLlBleLayoutParent = itemView.findViewById(R.id.ll_ble_layout_parent)
            }
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BleDeviceViewHolder {
        val view = LayoutInflater.from(ctx).inflate(viewType, parent, false)
        return BleDeviceViewHolder(view, viewType)
    }


    override fun onBindViewHolder(holder: BleDeviceViewHolder, position: Int) {
        val viewType = getItemViewType(position)
        if (viewType != R.layout.layout_no_data) {
            val bleDevice = bleDeviceList[position]
            holder.mBtnDeviceName?.text = bleDevice.device.name
            holder.mBtnDeviceMac?.text = bleDevice.device.address
            val rsi = "${bleDevice.rssi} dBm"
            holder.mTvBleDbm?.text = rsi
            holder.mBtnConnectBle?.setOnClickListener {
                //跳转到ble page活动
                EventBus.getDefault().post(DeviceFragment.DeviceAdapterEvent(bleDevice))
            }
        }
    }

    override fun onBindViewHolder(
        holder: BleDeviceViewHolder,
        position: Int,
        payloads: MutableList<Any>
    ) {
        if (payloads.isEmpty()) {
            super.onBindViewHolder(holder, position, payloads)
        } else {
            val device = bleDeviceList[position]
            payloads.forEach {
                when (it.toString()) {
                    payload_dBm -> {
                        val rsi = "${device.rssi} dBm"
                        holder.mTvBleDbm?.text = rsi
                        holder.mTvBleDbm?.setTextColorIn2S()
                    }
                }
            }
        }
    }




    override fun getItemCount(): Int {
        return if (bleDeviceList.isEmpty()) 1 else bleDeviceList.size
    }

    override fun getItemViewType(position: Int): Int {
        return if (bleDeviceList.isEmpty()) R.layout.layout_no_data else R.layout.item_ble_device
    }


    fun addDevice(btDevice: BLEDevice) {
        bleDeviceList.forEachIndexed { index, it ->
            if (it.device.address == btDevice.device.address) {
                it.scanRecord = btDevice.scanRecord
                if (it.rssi != btDevice.rssi) {
                    it.rssi = btDevice.rssi
                    Log.e(TAG, "addDevice: payload_dBm"  )
                    notifyItemChanged(index, payload_dBm)
                }
                return@addDevice
            }
        }
        bleDeviceList.add(btDevice)
        notifyItemChanged(bleDeviceList.size + 1)
    }

    fun clear(){
        bleDeviceList.clear()
        notifyDataSetChanged()
    }

    companion object {
        const val payload_dBm = "mTvBleDbm"
    }

}

